﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;

namespace CCAI.Office.PowerPoint.AddIn.Core
{
    /// <summary>
    /// This class facilitates reading CSV files
    /// </summary>
    public static class CSVReader
    {
        /// <summary>
        /// Converts each line into an array of strings, split by '|'
        /// </summary>
        /// <param name="path">Path of file to read.</param>
        /// <returns>Returns list of string array.</returns>
        public static List<string[]> ParseCSV(string path)
        {
            List<string[]> parsedData = new List<string[]>();

            try
            {
                using (StreamReader readFile = new StreamReader(path))
                {
                    string line;
                    string[] row;

                    while ((line = readFile.ReadLine()) != null)
                    {
                        row = line.Split('|');
                        parsedData.Add(row);
                    }
                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message);
                bool rethrow = ExceptionPolicy.HandleException(ex, "Global Policy");
                if (rethrow)
                    throw;
         }

            return parsedData;
        }
    }
}